package b1.scenes.tower
{
	import as3isolib.display.IsoSprite;
	import as3isolib.geom.*;
	
	import b1.conifg.story.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.fight.*;
	import b1.scenes.home.*;
	
	import com.greensock.*;
	import com.greensock.easing.Linear;
	
	import flash.display.Sprite;
	import flash.text.*;

	public class AnimalGrid extends IsoSprite
	{
		public var animalId:int;
		public var path:Array;
		private var _direction:int;
		private var moveFlag:Boolean;
		private var keys:Object;
		private var speakGrid:IsoSprite;
		
		public function AnimalGrid(animalId:int, direct:int = 0)
		{
			super(null);
			this.animalId = animalId;
			keys = new Object();
            var GridClass0:Class = ResourceHelper.getInstance().getClassByName("AnimalGrid1_0");
            var animalSprite0:Sprite = new GridClass0();
//            animalSprite0.visible = false;
            var GridClass1:Class = ResourceHelper.getInstance().getClassByName("AnimalGrid1_1");
            var animalSprite1:Sprite = new GridClass1();
//            animalSprite1.visible = true;
            this.sprites = [animalSprite0, animalSprite1];
            this.direction = direct;
		}
		
		public function startMove():void
		{
			if (!moveFlag)
			{
				onMove();	
			}
		}
		
		public function get currentGridX():int
		{
			return x / TowerLayer.gridWidth;
		}
		
		public function get currentGridY():int
		{
			return y / TowerLayer.gridWidth;
		}
		
		public function get direction():int
		{
			return _direction;	
		}
		
		public function set direction(direct:int):void
		{
			_direction = direct;
			switch (direct)
			{
				case 0:
					this.sprites[0].visible = true;
					this.sprites[1].visible = false;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = 1;
				break;
				case 1:
					this.sprites[0].visible = true;
					this.sprites[1].visible = false;
					this.sprites[0].scaleX = -1;
					this.sprites[1].scaleX = 1;
				break;
				case 2:
					this.sprites[0].visible = false;
					this.sprites[1].visible = true;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = -1;
				break;
				default:
					this.sprites[0].visible = false;
					this.sprites[1].visible = true;
					this.sprites[0].scaleX = 1;
					this.sprites[1].scaleX = 1;
			}
		}
		
		private function onStartMove(targetGrid:TowerGridConfig):Boolean
		{
			if (targetGrid == null)
				return false;
			if (targetGrid.lockId > 0 && keys[targetGrid.lockId] == null)
			{
				speak("门上锁了，需要" + Tower.keyNames[targetGrid.lockId - 1] + "钥匙。");
				return false;
			}
			else
			{
				return true;
			}
		}
		
		private function onMove():void
		{
			moveFlag = false;	
			if (path != null && path.length > 0)
			{
				var targetGrid:TowerGridConfig = path.pop();
				if (onStartMove(targetGrid))
				{
					TweenLite.to(this, 0.5, {x:targetGrid.posX * TowerLayer.gridWidth, y:targetGrid.posY * TowerLayer.gridWidth, ease:Linear.easeNone, onComplete:onReachGrid, onCompleteParams:[targetGrid]})
					if (currentGridX > targetGrid.posX)
					{
						direction = 2;
					}
					else if (currentGridX < targetGrid.posX)
					{
						direction = 0;
					}
					else if (currentGridY > targetGrid.posY)
					{
						direction = 3;
					}
					else if (currentGridY < targetGrid.posY)
					{
						direction = 1;
					}
					moveFlag = true;
				}
			}
		}
		
		private function onReachGrid(targetGrid:TowerGridConfig):void
		{
			var towerGrid:TowerGrid = Tower.getInstance().currentLayer.towerGrids[targetGrid.posX + "_" + targetGrid.posY];
			//出口
			if (targetGrid.type == 3)
			{
				SceneContainer.getInstance().loadPreviousScene();
				var home:Home = SceneContainer.getInstance().currentScene as Home;
				home.switchLiveId(targetGrid.exitLiveId);
				BottomNavigator.getInstance().hide();
				moveFlag = false;
			}
			//钥匙
			else if (targetGrid.keyId > 0 && towerGrid != null)
			{
				keys[targetGrid.keyId] = targetGrid.keyId;
				speak("获得" + Tower.keyNames[targetGrid.keyId - 1] + "钥匙");
				Tower.getInstance().currentLayer.removeTowerGrid(targetGrid.posX, targetGrid.posY);
				moveFlag = false;
			}
			//门
			else if (targetGrid.lockId > 0 && towerGrid != null)
			{
				speak("使用" + Tower.keyNames[targetGrid.lockId - 1] + "钥匙打开了门");
				Tower.getInstance().currentLayer.removeTowerGrid(targetGrid.posX, targetGrid.posY);
				moveFlag = false;
			}
			//动物
			else if (targetGrid.animalId < 0)
			{
				targetGrid.fightConfig.userData = towerGrid;
				startFight(targetGrid.fightConfig);
				moveFlag = false;
			}
			else
			{
				onMove();
			}
		}
		
		public function speak(content:String):void
		{
			if (speakGrid == null)
			{
				var AnimalMsgBgr:Class = ResourceHelper.getInstance().getClassByName("DialogMsgBgrImage");
				var speakMessage:Sprite = new AnimalMsgBgr();
				speakMessage.height = 60;
				speakMessage.scaleX = speakMessage.scaleY;
				speakMessage.x = 0;
				speakMessage.y = -speakMessage.height;
				
				var speakTextContainer:Sprite = new Sprite();
				var speakText:TextField = new TextField();
				speakText.selectable = false; 
	        	speakText.autoSize = TextFieldAutoSize.LEFT;
				speakText.defaultTextFormat = new TextFormat("微软雅黑", 22, 0x000000);
				speakText.width = 170;
				speakText.wordWrap = true;
				speakText.x = 18;
				speakText.y = 2;
				speakText.text = content;
				speakMessage.addChild(speakText);
				
				var tower:Tower = Tower.getInstance();
				speakGrid = new IsoSprite();
				var gridWidth:int = TowerLayer.gridWidth;
				speakGrid.setSize(gridWidth, gridWidth, gridWidth);
				speakGrid.moveBy(this.x, this.y, this.z + gridWidth);                        
				tower.currentLayer.isoScene.addChild(speakGrid);
				speakGrid.sprites = [speakMessage];
	
				TweenLite.delayedCall(3, removeSpeakMessage);
			}
		}
		
		private function removeSpeakMessage():void
		{
			if (speakGrid != null && speakGrid.parent != null)
			{
				speakGrid.parent.removeChild(speakGrid);
				speakGrid = null;
			}
		}
		
		//开始战斗
		private function startFight(fightConfig:FightConfig):void
		{
			//本人信息
			var userId:int = PlayerInfoHelper.getInstance().userId;
			var userInfo:PlayerInfo = PlayerInfoHelper.getInstance().players[userId];
			var userAnimalId:int = userInfo.ownAnimals[0];
			//对手信息
			var opponentAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[fightConfig.opponentAnimalId];
			var fight:Fight = new Fight(userId, opponentAnimal.ownerId, [userAnimalId], [fightConfig.opponentAnimalId], onFightComplete, fightConfig);
			SceneContainer.getInstance().loadScene(fight);
		}
		
		//战斗结束回调
		private function onFightComplete(leftPlayerId:int, rightPlayerId:int, winnerAnimalId:int, loserAnimalId:int, caseBones:int, loseHeirloomId:String, 
			animalCluesDrop:Object, fightConfig:FightConfig, newHonours:Object):void
		{
//			trace(SceneContainer.getInstance().currentScene);
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			//失败
			if (fightConfig.failTaskFlag && winnerAnimalId != parseInt(me.ownAnimals[0]))
			{
				if (StoryHelper.getInstance().storyModeFlag)
				{
					
				}
				else
				{
				}
//				storyModeFlag = false;
//				SceneContainer.getInstance().mouseDisableFlag = true;
//				SceneContainer.getInstance().targetPosX = 352.2;
//				SceneContainer.getInstance().targetPosY = 345.95;
//				var helperMessageBox:MessageBoxWithIcon = new MessageBoxWithIcon("任务失败！self 探长不要灰心，请回去休整之后再来！", "id:-5", Config.assistantName);
//				helperMessageBox.x = BFont.stageWidth / 2;
//				helperMessageBox.y = BFont.stageHeight  / 2;
//				helperMessageBox.buttonOK.addEventListener(MouseEvent.CLICK, onFailButtonOKClick);
//				SceneContainer.getInstance().currentScene.addChild(helperMessageBox);
			}
			//胜利
			else
			{
				var animalGrid:AnimalGrid = Tower.getInstance().currentLayer.animalGrids[loserAnimalId];
				animalGrid.flyOut();
			}			
		}
		
		public function flyOut():void
		{
			TweenLite.to(this, 0.5, {x:-40, y:-40, z:100, onComplete:destory})
		}
		
		public function destory():void
		{
			this.parent.removeChild(this);
		}
		
	}
}